package com.zzx.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.Collections;

public class CodeGenerator {


    public static void main(String[] args) {

        String basePackage = "com.zzx";
        String tableName = "t_product_gx_submit";
        if (tableName.isEmpty()) {
            System.err.println("请输入表名！");
            return;
        }
        // 获取项目根路径
        String projectPath = System.getProperty("user.dir");

        // 创建配置
        GeneratorConfig config = new GeneratorConfig()
                .jdbcUrl("jdbc:mysql://47.120.15.131:3388/jj?useUnicode=true&characterEncoding=utf-8&useSSL=true")
                .username("root")
                .password("1qws!QWS")
                .author("zzx")
                .outputDir(projectPath + "/src/main/java")
                .basePackage(basePackage)
                .tableNames(tableName)
                .tablePrefix("")
                .xmlPath(projectPath + "/src/main/resources/mapper")
                .enableFileOverride(true);

        // 执行生成
        generate(config);

        System.out.println("代码生成完成!");
    }

    /**
     * 代码生成入口方法
     */
    public static void generate(GeneratorConfig config) {
        FastAutoGenerator.create(config.getJdbcUrl(), config.getUsername(), config.getPassword())
                .globalConfig(builder -> {
                    builder.author(config.getAuthor())
                            .outputDir(config.getOutputDir())
                            .enableSwagger()
                            .disableOpenDir();

                    // 文件覆盖配置
                    if (config.isEnableFileOverride()) {
                        builder.fileOverride();
                    }

                })
                .packageConfig(builder -> {
                    builder.parent(config.getBasePackage())
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(
                                    OutputFile.xml,
                                    config.getXmlPath()
                            ));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(config.getTableNames().split(","))
                            .addTablePrefix(config.getTablePrefix())

                            // Entity 配置
                            .entityBuilder()
                            .enableLombok()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .enableTableFieldAnnotation()
                            .superClass(config.getSuperEntityClass())
                            .addSuperEntityColumns(config.getSuperEntityColumns().split(","))
                            .enableActiveRecord()
                            .logicDeleteColumnName("deleted")
                            .versionColumnName("version")
                            .addIgnoreColumns("deleted")

                            // Controller 配置
                            .controllerBuilder()
                            .enableRestStyle()
                            .formatFileName("%sController")

                            // Service 配置
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")

                            // Mapper 配置
                            .mapperBuilder()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper");
                })
                .templateConfig(builder -> {
                    builder.controller("/template/controller.java.vm")
                            .service("/template/service.java.vm")
                            .serviceImpl("/template/serviceImpl.java.vm")
                            .entity("/template/entity.java.vm")
                            .mapper("/template/mapper.java.vm")
                            .xml("/template/mapper.xml.vm");
                })
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }

    /**
     * 配置参数对象
     */
    public static class GeneratorConfig {
        private String jdbcUrl;
        private String username;
        private String password;
        private String author;
        private String outputDir;
        private String basePackage;
        private String tableNames;
        private String tablePrefix;
        private String superEntityClass = "com.zzx.entity.BaseEntity";
        private String superEntityColumns = "create_by,update_by,create_time,update_time,deleted,company_id";
        private String xmlPath;
        private boolean enableFileOverride = true;

        public GeneratorConfig jdbcUrl(String jdbcUrl) {
            this.jdbcUrl = jdbcUrl;
            return this;
        }
        public GeneratorConfig username(String username) {
            this.username = username;
            return this;
        }
        public GeneratorConfig password(String password) {
            this.password = password;
            return this;
        }
        public GeneratorConfig author(String author) {
            this.author = author;
            return this;
        }
        public GeneratorConfig outputDir(String outputDir) {
            this.outputDir = outputDir;
            return this;
        }
        public GeneratorConfig basePackage(String basePackage) {
            this.basePackage = basePackage;
            return this;
        }
        public GeneratorConfig tableNames(String tableNames) {
            this.tableNames = tableNames;
            return this;
        }
        public GeneratorConfig tablePrefix(String tablePrefix) {
            this.tablePrefix = tablePrefix;
            return this;
        }
        public GeneratorConfig superEntityClass(String superEntityClass) {
            this.superEntityClass = superEntityClass;
            return this;
        }
        public GeneratorConfig superEntityColumns(String superEntityColumns) {
            this.superEntityColumns = superEntityColumns;
            return this;
        }
        public GeneratorConfig xmlPath(String xmlPath) {
            this.xmlPath = xmlPath;
            return this;
        }
        public GeneratorConfig enableFileOverride(boolean enableFileOverride) {
            this.enableFileOverride = enableFileOverride;
            return this;
        }

        public String getJdbcUrl() { return jdbcUrl; }
        public String getUsername() { return username; }
        public String getPassword() { return password; }
        public String getAuthor() { return author; }
        public String getOutputDir() { return outputDir; }
        public String getBasePackage() { return basePackage; }
        public String getTableNames() { return tableNames; }
        public String getTablePrefix() { return tablePrefix; }
        public String getSuperEntityClass() { return superEntityClass; }
        public String getSuperEntityColumns() { return superEntityColumns; }
        public String getXmlPath() { return xmlPath; }
        public boolean isEnableFileOverride() { return enableFileOverride; }
    }


}
